package com.educate.common.api;

/**
 * @author yjj
 * @Description 通用返回对象
 * @createTime 2021/09/05
 */
public class Result<T> {
    //状态码
    private long code;
    //提示信息
    private String msg;
    //数据封装
    private T data;

    public Result() {
    }

    public Result(long code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回的结果
     *
     * @param data
     * @param <T>
     * @return 处理后的数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回的结果
     *
     * @param data
     * @param code
     * @param msg
     * @param <T>
     * @return 处理后的数据
     */
    public static <T> Result<T> success(T data, long code, String msg) {
        return new Result<>(code, msg, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed(IErrorCode errorCode) {
        return new Result<>(errorCode.getCode(), errorCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode
     * @param msg       提示信息
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed(IErrorCode errorCode, String msg) {
        return new Result<>(errorCode.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed(String msg) {
        return new Result<>(ResultCode.FAILED.getCode(), msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed() {
        return failed(ResultCode.FAILED);
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
